<?php
/**
 * Created by PhpStorm.
 * User: tengwei
 * Date: 2018/11/16
 * Time: 3:36 PM
 */

namespace common\logics\message;

use common\services\message\ChuanglanSmsApi;
use common\widgets\Request;
use common\widgets\Tool;
use yii\base\BaseObject;
use Yii;

class MesgLogic extends BaseObject implements IMessage
{
    /** @var \common\sources\read\message\MesgAccess $RMesgAccess */
    private $RMesgAccess;
    /** @var \common\sources\write\message\MesgAccess $WMesgAccess */
    private $WMesgAccess;
    /** @var \common\sources\read\buyuser\BuyUserAccess $RBuyUserAccess */
    private $RBuyUserAccess;


    public function init()
    {
        $this->RMesgAccess = Yii::$container->get('RMesgAccess');
        $this->WMesgAccess = Yii::$container->get('WMesgAccess');
        $this->RBuyUserAccess = Yii::$container->get('RBuyUserAccess');
        parent::init();
    }

    public function smsApi($data)
    {
        $clapi = new ChuanglanSmsApi();
        $verifycode = rand(100000, 999999);
        if(empty($data['phone'])){
            return Tool::returnJson($data = [], $code = 401, '发送短信手机号码不能为空');
        }
        if(empty($data['smstype'])){
            return Tool::returnJson($data = [], $code = 402, '发送短信类型不能为空');
        }
        if(!Tool::isPhone($data['phone'])){
            return Tool::returnJson($data = [], $code = 403, '手机号码类型有误');
        }
        //登陆时检测是否注册
        if ($data['smstype'] == 'login' || $data['smstype'] == 'forget_pwd' || $data['smstype'] == 'update_pwd' || $data['smstype'] == 'take_addr' || $data['smstype'] == 'set_pay_pwd') {
            $buyer = $this->RBuyUserAccess->getBuyUser($data['phone']);
            if (empty($buyer)) {
                return Tool::returnJson($data = [], $code = 405, '手机号还未注册，请先注册后操作');
            }
        }
        $data['verifycode'] = $verifycode;
        $data['sendtime'] = time();
        $data['smstype'] = Yii::$app->params['sms_config']['sms_type'][$data['smstype']];

        //更改发送短文字信息
        if ($data['smstype']=='0'){
            $msg = Yii::$app->params['sms_config']['sms_msg']['reg_msg'];
        }
        if ($data['smstype']=='1'){
            $msg = Yii::$app->params['sms_config']['sms_msg']['login_msg'];
        }
        if($data['smstype'] == '4'){
            $msg = Yii::$app->params['sms_config']['sms_msg']['update_pwd_msg'];
        }
        if($data['smstype'] == '3'){
            $msg = Yii::$app->params['sms_config']['sms_msg']['forget_pwd_msg'];
        }
        if ($data['smstype']=='5'){
            $msg = Yii::$app->params['sms_config']['sms_msg']['bank_msg'];
        }
        if($data['smstype']=='6'){
            $msg = Yii::$app->params['sms_config']['sms_msg']['take_addr_msg'];
        }
        if($data['smstype'] == '7'){
            $msg = Yii::$app->params['sms_config']['sms_msg']['set_pay_pwd'];
        }
        if(empty($msg)){
            return Tool::returnJson($data = [], $code = 406, '短信验证码内容不能为空');
        }
        //短信发送记录检测
        $smsNumber = $this->smsAuth($data);
        if ($smsNumber >= Yii::$app->params['sms_config']['sms_max']) {
            return Tool::returnJson($data = [], $code = 407, '今日验证码发送次数已达上限');
        }

        try{
            $params = $data['phone'] . "," . $verifycode;
            //调用短信发送api
            $result = $clapi->sendVariableSMS($msg, $params);
            if (!is_null(json_decode($result))) {
                $output = json_decode($result, true);
                if (isset($output['code']) && $output['code'] == '0') {
                    $this->WMesgAccess->saveCode($data);
                    return Tool::returnJson($data = [], $code = 200, '发送成功');
                } else {
                    $msg = $output['errorMsg'];
                    return Tool::returnJson($data = [], $code = 408, $msg);
                }
            } else {
                return Tool::returnJson($data = [], $code = 409, $result);
            }
        }catch (\Exception $e){
            return Tool::returnJson([],510, "发送短信验证码异常".$e->getMessage());
        }
    }

    //发送记录检测方法
    public function smsAuth($data)
    {
        $data['today'] = strtotime(date("Y-m-d"), time());
        $data['endtoday'] = $data['today'] + 60 * 60 * 24;
        $smsNumber = $this->RMesgAccess->getSms($data);
        return $smsNumber;
    }
}